#include <iostream>
#include <unistd.h>
#include "../../../plcc/thread/Thread.h"
#include "../../../plcc/thread/Mutex.h"
#include "../../../plcc/thread/Event.h"
using namespace std;

class MyThread :public Thread {
  virtual void Run() { 
    for (int i=0; i<10; i++) {
      AutoMutex<Mutex>tmp(m_);
      cout << "wait" << endl;
      e_.Wait(m_);
      cout << "now comes thread 1::" << i<< endl; 
    }
  }
 private:
  Event e_;
  Mutex m_;
  
 public:
  void signal() {
    AutoMutex<Mutex>tmp(m_);
    e_.Signal();
  }
};

int main () {
  MyThread *p = new MyThread;
  p->Start();
  cout << "main thread" << endl;
  sleep(10);
  for(int i =0; i< 10; i++) {
    cout << "signal" << i <<endl;
    p->signal();
    sleep(2);
  }
  p->Join();
  cout << "joined" << endl;
}
